var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
const app = getApp()
// 触底上拉刷新 TODO 这里要将page传给服务器，作者没写
Page({
    data: {
        // 设备机型
        isIphoneX: 0,
        // 当前显示订单列表
        diyOrderList: [],
        // 全部订单列表
        alldiyOrderList: [],
        // 订单页数
        allPage: 1,
        // 订单总条数
        allCount: 0,
        // 要显示的页数
        page: 1,
        // 每页要显示的条数
        size: 8,
        // 当前显示的数据类型
        showType: 0,
        hasOrder: 0,
        // 是否还有更多数据
        showTips: 0,
        // 订单状态数据数量列表
        // status: {},
        // 页面高度
        sysHeight: 0,
        // 控制加载
        loading: 0,
    },

    // 用户点击确认收货
    confirmGetGoods(e) {
        let that = this;
        let orderId = e.currentTarget.dataset.orderid;
        wx.showModal({
            title: '提示',
            content: '您要确认收货吗',
            success(res) {
                if (res.confirm) {
                    util.requestApi(api.setDiyOrderStatus, {
                        orderID: orderId,
                        type:1,
                        operation:3
                    }).then(function (res) {
                        if (res.code == 200) {
                            that.setData({
                                page: 1,
                                size: 8,
                                count: 0,
                                allPage: 1,
                                diyOrderList: [],
                            })
                            wx.showToast({
                                title: '成功',
                                icon: 'none',
                                duration: 2000
                            })
                            that.getUserOrder(that.data.showType)
                        }else{
                            wx.showToast({
                                title: '收货失败',
                                icon: 'none',
                                duration: 2000
                            })
                        }
                    })
                }
            }
        })
    },
    // 获取用户订单列表
    getUserOrder(status) {
        let that = this
        let userID = wx.getStorageSync('userID')
        util.requestApi(api.getCustomOrder, {
            userID: userID,
            status: parseInt(status),
            page: that.data.page,
            size: that.data.size
        }).then(function (res) {
            if (res.code == 200) {
                if (res.content.data.length > 0) {
                    let list = res.content.data;
                    that.setData({
                        allCount: res.content.count,
                        allPage: Math.ceil(res.content.count / that.data.size),
                        diyOrderList: that.data.diyOrderList.concat(list),
                        showTips: list.length < that.data.size ? 1 : 0,
                        loading: 1
                    });
                } else {
                    that.setData({
                        hasOrder: 1,
                        loading: 1
                    });
                    wx.showToast({
                        title: '无相关订单，快去下单吧',
                        icon: 'none',
                        duration: 2000
                    })
                }

            }
        })
    },
    // 跳转首页
    toIndexPage: function (e) {
        wx.switchTab({
            url: '/pages/index/index'
        });
    },
    onLoad: function () {
        // 获取设备信息
        let info = wx.getSystemInfoSync();
        let sysHeight = info.windowHeight - 100;
        this.setData({
            sysHeight: sysHeight,
        });
        // 获取设备机型信息
        let phone = wx.getStorageSync('isIphoneX');
        if (phone) {
            this.setData({
                isIphoneX: 1
            })
        }
    },
    // 进入该界面 获取该界面的tab值 获取订单列表
    onShow: function () {
        this.setData({
            showType: 0,
            diyOrderList: [],
            alldiyOrderList: [],
            allPage: 1,
            allCount: 0,
            size: 8
        });
        this.getUserOrder(this.data.showType)
    },
    // 切换tab
    switchTab: function (event) {
        let showType = event.currentTarget.dataset.index;
        this.setData({
            showType: showType,
            diyOrderList: [],
            alldiyOrderList: [],
            page: 1,
        });
        this.getUserOrder(showType);
    },
    // “取消订单”点击效果
    cancelOrder: function (e) {
        let that = this;
        let orderId = e.currentTarget.dataset.orderid;
        wx.showModal({
            title: '',
            content: '确定要取消此订单？',
            success: function (res) {
                if (res.confirm) {
                    util.requestApi(api.setDiyOrderStatus, {
                        orderID: orderId,
                        type:1,
                        operation:2
                    }, 'POST').then(function (res) {
                        if (res.code === 200) {
                            wx.showToast({
                                title: '取消订单成功'
                            });
                            that.setData({
                                page: 1,
                                size: 8,
                                count: 0,
                                allPage: 1,
                                diyOrderList: [],
                            });
                            that.getUserOrder(that.data.showType);
                        } else {
                            util.showErrorToast(res.msg);
                        }
                    });
                }
            }
        });
    },
    // 申请退款
    applyRef(e){
        let that = this;
        let orderId = e.currentTarget.dataset.orderid;
        wx.showModal({
            title: '',
            content: '确定要申请退款吗？',
            success: function (res) {
                if (res.confirm) {
                    util.requestApi(api.orderRefund, {
                        orderID: orderId,
                        type:1
                    }, 'POST').then(function (res) {
                        if (res.code === 200) {
                            wx.showToast({
                                title: '申请退款成功'
                            });
                            that.setData({
                                page: 1,
                                size: 8,
                                count: 0,
                                allPage: 1,
                                diyOrderList: [],
                            });
                            that.getUserOrder(that.data.showType);
                        } else {
                            util.showErrorToast(res.msg);
                        }
                    });
                }
            }
        });
    },
    // 删除订单
    delOrder(e){
        let that = this;
        let orderId = e.currentTarget.dataset.orderid;
        wx.showModal({
            title: '',
            content: '确定要删除此订单？',
            success: function (res) {
                if (res.confirm) {
                    util.requestApi(api.setDiyOrderStatus, {
                        orderID: orderId,
                        type:1,
                        operation:1
                    }, 'POST').then(function (res) {
                        if (res.code === 200) {
                            wx.showToast({
                                title: '删除订单成功'
                            });
                            that.setData({
                                page: 1,
                                size: 8,
                                count: 0,
                                allPage: 1,
                                diyOrderList: [],
                            });
                            that.getUserOrder(that.data.showType);
                        } else {
                            util.showErrorToast(res.msg);
                        }
                    });
                }
            }
        });
    },
    // 控制订单数据是否存在更多
    onReachBottom: function () {
        let that = this;
        if (that.data.page >= that.data.allPage) {
            that.setData({
                showTips: 1
            });
            return false;
        }
        that.setData({
            page: that.data.page + 1
        });
        that.getUserOrder(that.data.showType);
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        wx.showNavigationBarLoading()
        this.setData({
            page: 1,
            size: 8,
            count: 0,
            allPage: 1,
            diyOrderList: [],
            loading: 0
        })
        this.getUserOrder(this.data.showType);
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
    },
     // 点击订单支付
     payOrder: function (e) {
        let that = this;
        let userID=wx.getStorageSync('userID');
        let id = e.currentTarget.dataset.orderid;
        wx.showModal({
            title: '提示',
            content: '确认支付该订单',
            success(res) {
                if (res.confirm) {
                    util.requestApi(api.createCustomOrder, {
                        userID: userID,
                        customOrderID:id
                    }).then(function (res) {
                        if (res.code == 200) {
                            let con = res.content
                            wx.requestPayment({
                                timeStamp: con.timeStamp,
                                nonceStr: con.nonceStr,
                                package: con.package,
                                signType: con.signType,
                                paySign: con.paySign,
                                success(res) {
                                    wx.showToast({
                                        title: '下单成功',
                                        icon: 'success',
                                        duration: 2000
                                    })
                                    that.getUserOrder(that.data.showType);
                                },
                                fail(res) {
                                    wx.showToast({
                                        title: '下单失败',
                                        icon: 'none',
                                        duration: 2000
                                    })
                                }
                            })
                        } else {
                            wx.showToast({
                                title: res.msg,
                                icon: 'none',
                                duration: 2000
                            })
                        }
                    })
                } else if (res.cancel) {
                    wx.showToast({
                        title: '取消下单',
                        icon: 'success',
                        duration: 2000
                    })
                }
            }
        })
    },
    // 查看订单详情
    goDetail(e){
        let orderID=e.currentTarget.dataset.orderid;
        wx.navigateTo({
          url: '/pages/ucenter/order-details/index?orderID='+orderID+'&type='+1,
        })
    }
})